package com.bdzl.admin.controller.sys;

import java.util.List;
import java.util.Map;

import com.bdzl.admin.service.data.EnterpriseService;
import com.bdzl.admin.service.data.FddCaEntService;
import com.bdzl.admin.service.sys.OrgEntService;
import com.bdzl.admin.service.sys.OrgFddcaService;
import com.bdzl.pub.controller.BaseController;
import com.bdzl.pub.entity.data.EnterpriseDO;
import com.bdzl.pub.entity.data.FddCaEntDO;
import com.bdzl.pub.entity.sys.OrgEntDO;
import com.bdzl.pub.entity.sys.OrgFddcaDO;
import com.bdzl.pub.vo.sys.OrgVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bdzl.pub.entity.sys.OrgDO;
import com.bdzl.admin.service.sys.OrgService;
import com.bdzl.pub.util.param.PageUtils;
import com.bdzl.pub.util.param.Query;
import com.bdzl.pub.util.param.R;

/**
 * 组织机构表
 * 
 * @author yangyicai
 * @email yangyicai@foxmail.com
 * @date 2021-03-23 13:59:22
 */
 
@Controller
@RequestMapping("/bdzl/org")
public class OrgController extends BaseController {
	@Autowired
	private OrgService orgService;
	@Autowired
	private OrgEntService orgEntService;
	@Autowired
	private EnterpriseService enterpriseService;
	@Autowired
	private OrgFddcaService orgFddcaService;
	@Autowired
	private FddCaEntService fddCaEntService;

	
	@GetMapping()
	String Org(){
	    return "bdzl/org/org";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<OrgDO> orgList = orgService.list(query);
		int total = orgService.count(query);
		PageUtils pageUtils = new PageUtils(orgList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "bdzl/org/add";
	}

	@PostMapping("/edit")
	@ResponseBody
	OrgVO edit(@RequestBody OrgDO org){
		OrgVO orgVO = new OrgVO();
		org = orgService.get(org.getOrgId());
		//类拷贝
		BeanUtils.copyProperties(org, orgVO);
		//获取机构和企业信息关联
		OrgEntDO bySoeOrg = orgEntService.getBySoeOrg(org.getOrgId());
		//获取对应企业信息
		EnterpriseDO enterpriseDO = enterpriseService.get(bySoeOrg.getSoeEnt());
		orgVO.setEnterpriseDO(enterpriseDO);
		//获取机构和法大大关系表
		OrgFddcaDO byOrgId = orgFddcaService.getByOrgId(Integer.valueOf(org.getOrgId()));
		//获取机构法大大CA证书
		FddCaEntDO fddCaEntDO = fddCaEntService.get(byOrgId.getFddCaId());
		orgVO.setFddCaEntDO(fddCaEntDO);
		return orgVO;
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( OrgDO org){
		if(orgService.save(org)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update(@RequestBody OrgVO org){
		orgService.update(org);
		EnterpriseDO enterpriseDO = org.getEnterpriseDO();//企业信息
		//获取机构和企业信息关联
		OrgEntDO bySoeOrg = orgEntService.getBySoeOrg(org.getOrgId());
		if (bySoeOrg==null){
			enterpriseService.save(enterpriseDO);//保存企业信息
			bySoeOrg = new OrgEntDO();
			bySoeOrg.setSoeOrg(org.getOrgId());
			bySoeOrg.setSoeId(enterpriseDO.getEntId());
			orgEntService.save(bySoeOrg);//保存关系表
		}else {
			enterpriseService.update(enterpriseDO);
		}
		FddCaEntDO fddCaEntDO = org.getFddCaEntDO();//法大大
        //获取机构和法大大关系表
		OrgFddcaDO byOrgId = orgFddcaService.getByOrgId(Integer.valueOf(org.getOrgId()));
		if (byOrgId==null){
			fddCaEntService.save(fddCaEntDO);//保存法大大
			byOrgId = new OrgFddcaDO();
			byOrgId.setOrgId(Integer.valueOf(org.getOrgId()));
			byOrgId.setFddCaId(fddCaEntDO.getPceId());
			orgFddcaService.save(byOrgId);  //保存机构和法大大关系表
		}else {
			fddCaEntService.update(fddCaEntDO);
		}
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( String orgId){
		if(orgService.remove(orgId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") String[] orgIds){
		orgService.batchRemove(orgIds);
		return R.ok();
	}

}
